<?php
class News_model extends CI_Model {

	public function __construct()
	{
		$this->load->database();
	}
	
	public function login()
	{
		$username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));
        
        // Prep the query
        $this->db->where('user', $username);
        $this->db->where('password', $password);
        
        // Run the query
        $query = $this->db->get('admin');
        // Let's check if there are any results
        if($query->num_rows == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
                    'id' => $row->id,
                    'fname' => $row->fname,
                    'lname' => $row->lname,
                    'username' => $row->user,
                    'validated' => true
                    );
            $this->session->set_userdata($data);
            return true;
        }
        // If the previous process did not validate
        // then return false.
        return false;
    }
		
	
	public function get_news($table = 'news', $slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get($table);
			return $query->result_array();
		}
	
		$query = $this->db->get_where($table, array('slug' => $slug));
		return $query->row_array();
	}
	
	public function set_report() //adds incidents reported by students to database.  
	{
		$this->load->helper('url');
	
		$slug = url_title($this->input->post('title'), 'dash', TRUE);
	
		$data = array(
				'title' => $this->input->post('title'),
				'slug' => $slug,
				'text' => $this->input->post('text')
		);
	
		return $this->db->insert('reports', $data);
	}
	
	public function set_news()
	{
		$this->load->helper('url');
	
		$slug = url_title($this->input->post('title'), 'dash', TRUE);
//		$table = $this->input->post('table');
		$data = array(
				'title' => $this->input->post('title'),
				'slug' => $slug,
				'text' => $this->input->post('text')
		);
	
		return $this->db->insert($this->input->post('news_type'), $data);
	}
	
	public function change_password()
	{
		$old_password = $this->security->xss_clean($this->input->post('old_password'));
		$new_password = $this->security->xss_clean($this->input->post('new_password'));
		$confirm_password = $this->security->xss_clean($this->input->post('confirm_password'));
		$user_data = $this->session->get_userdata();
		
		//prep the query
		$this->db->where('user', $user_data['username']);
		$this->db->where('password', $old_password);
		
		// Run the query
		$query = $this->db->get('admin');
		if($query->num_rows == 1)
		{
			if ($new_password == $confirm_password)
			{
				$this->db->query("update admin set password = '".$new_password."' where user = '".$user_data['username']."'");
				//change password
				return true;
			}
			else 
			{
				return false;
				//new and confirm password don't match
			}
		}
		
		else
		{
			return false;
			//old password entered incorrectly. 
		} 
	}
	
	public function forgot_password()
	{
		$username = $this->security->xss_clean($this->input->post('username'));
		$this->db->where('user', $username);
		// Run the query
		$query = $this->db->get('admin');
		if($query->num_rows == 1)
		{
			//mail password to the user's e-mail ID.
			return true; 
		}
		else
		{
			return false;
		}
	}
	
	
}